unit echelper;
{ Helper functions for event commands }

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpjson, ec_base;

type
  TEcFrameClass = class of TEcBaseFrame;

function CreateEmptyECList: TJSONArray;
function CreateEmptyBattleEventPage: TJSONObject;

function GetEditorForEc(Code: Integer): TEcFrameClass; overload;
function GetEditorForEc(Ec: TJSONObject): TEcFrameClass; overload;

function IsCommandWithoutParameters(Code: Integer): Boolean;

implementation

uses
  constants,
  ec_showmessage, ec_showchoices, ec_inputnumber, ec_selectitem, ec_scrolltext,
  ec_setswitches, ec_setvariables, ec_setselfswitch, ec_controltimer,
  ec_condbranch, ec_callcommonevent, ec_label, ec_goto, ec_comment,

  ec_changegold, ec_changeitems,

  ec_plugincommand;

function CreateEmptyECList: TJSONArray;
begin
  CreateEmptyECList := TJSONArray.Create([
      TJSONObject.Create([
        'code', 0,
        'indent', 0,
        'parameters', TJSONArray.Create
      ])
    ])
end;

function CreateEmptyBattleEventPage: TJSONObject;
var
  Page: TJSONObject;

  function CreateConditions: TJSONObject;
  begin
    CreateConditions := TJSONObject.Create([
      'actorHp', 50,
      'actorId', 1,
      'actorValid', False,
      'enemyHp', 50,
      'enemyIndex', 0,
      'enemyValid', False,
      'switchId', 1,
      'switchValid', False,
      'turnA', 0,
      'turnB', 0,
      'turnEnding', False,
      'turnValid', False
    ]);
  end;

begin
  Page := TJSONObject.Create;
  Page.Add('conditions', CreateConditions);
  Page.Add('list', CreateEmptyECList);
  Page.Add('span', 0);

  CreateEmptyBattleEventPage := Page;
end;

function GetEditorForEc(Code: Integer): TEcFrameClass;
begin
  case Code of
    SHOW_MESSAGE_START_EC_CODE: Result := TECShowMessageFrame;
    SHOW_CHOICES_START_EC_CODE: Result := TECShowChoicesFrame;
    INPUT_NUMBER_EC_CODE: Result := TECInputNumberFrame;
    SELECT_ITEM_EC_CODE: Result := TECSelectItemFrame;
    SCROLLING_TEXT_START_EC_CODE: Result := TECScrollTextFrame;
    SET_SWITCHES_EC_CODE: Result := TECSetSwitchesFrame;
    SET_VARIABLES_EC_CODE: Result := TECSetVariablesFrame;
    SET_SELF_SWITCH_EC_CODE: Result := TECSelfSwitchFrame;
    CONTROL_TIMER_EC_CODE: Result := TECControlTimerFrame;
    CONDITIONAL_BRANCH_IF_EC_CODE: Result := TECCondBranchFrame;
    COMMON_EVENT_EC_CODE: Result := TECCallCommonEventFrame;
    LABEL_EC_CODE: Result := TECLabelFrame;
    GOTO_EC_CODE: Result := TECGotoFrame;
    COMMENT_EC_CODE: Result := TECCommentFrame;

    CHANGE_GOLD_EC_CODE: Result := TECChangeGoldFrame;
    CHANGE_ITEMS_EC_CODE: Result := TECChangeItemsFrame;
    {
      CHANGE_GOLD_EC_CODE = 125;
      CHANGE_ITEMS_EC_CODE = 126;
      CHANGE_WEAPONS_EC_CODE = 127;
      CHANGE_ARMOURS_EC_CODE = 128;
      CHANGE_PARTY_MEMBER_EC_CODE = 129;

      CHANGE_ACTOR_HP_EC_CODE = 311;
      CHANGE_ACTOR_MP_EC_CODE = 312;
      CHANGE_ACTOR_TP_EC_CODE = 326;
      CHANGE_ACTOR_STATE_EC_CODE = 313;
      COMPLETE_ACTOR_RECOVER_EC_CODE = 314;
      CHANGE_EXPERIENCE_EC_CODE = 315;
      CHANGE_LEVEL_EC_CODE = 316;
      CHANGE_PARAM_EC_CODE = 317;
      CHANGE_SKILL_EC_CODE = 318;
      CHANGE_EQUIPMENT_EC_CODE = 319;
      CHANGE_NAME_EC_CODE = 320;
      CHANGE_CLASS_EC_CODE = 321;
      CHANGE_NICKNAME_EC_CODE = 324;
      CHANGE_PROFILE_EC_CODE = 325;

      //Page 2
      TRANSFER_PLAYER_EC_CODE = 201;
      SET_VEHICLE_LOCATION_EC_CODE = 202;
      SET_EVENT_LOCATION_EC_CODE = 203;
      SCROLL_MAP_EC_CODE = 204;
      SET_MOVEMENT_ROUTE_EC_CODE = 205;
      SET_MOVEMENT_ROUTE_SUBCOMMAND_EC_CODE = 505;
      GET_ON_OFF_VEHICLE_EC_CODE = 206;

      CHANGE_TRANSPARENCY_EC_CODE = 211;
      CHANGE_PLAYER_FOLLOWERS_EC_CODE = 216;
      GATHER_FOLLOWERS_EC_CODE = 217;
      SHOW_ANIMATION_EC_CODE = 212;
      SHOW_BALLOON_ICON_EC_CODE = 213;
      ERASE_EVENT_EC_CODE = 214;

      SHOW_PICTURE_EC_CODE = 231;
      MOVE_PICTURE_EC_CODE = 232;
      ROTATE_PICTURE_EC_CODE = 233;
      TINT_PICTURE_EC_CODE = 234;
      ERASE_PICTURE_EC_CODE = 235;

      WAIT_EC_CODE = 230;

      FADEOUT_SCREEN_EC_CODE = 221;
      FADEIN_SCREEN_EC_CODE = 222;
      TINT_SCREEN_EC_CODE = 223;
      FLASH_SCREEN_EC_CODE = 224;
      SHAKE_SCREEN_EC_CODE = 225;
      SET_WEATHER_EFFECT_EC_CODE = 236;

      PLAY_BGM_EC_CODE = 241;
      FADEOUT_BGM_EC_CODE = 242;
      SAVE_BGM_EC_CODE = 243;
      REPLAY_BGM_EC_CODE = 244;
      PLAY_BGS_EC_CODE = 245;
      FADEOUT_BGS_EC_CODE = 246;
      PLAY_ME_EC_CODE = 249;
      PLAY_SE_EC_CODE = 250;
      STOP_SE_EC_CODE = 251;
      PLAY_MOVIE_EC_CODE = 261;

      //Page 3
      BATTLE_PROCESSING_EC_CODE = 301;
      BATTLE_PROCESSING_WIN_EC_CODE = 601;
      BATTLE_PROCESSING_ESCAPE_EC_CODE = 602;
      BATTLE_PROCESSING_LOSE_EC_CODE = 603;
      BATTLE_PROCESSING_END_EC_CODE = 604;

      SHOP_PROCESSING_EC_CODE = 302;
      SHOP_PROCESSING_LINE_EC_CODE = 605;

      NAME_INPUT_PROCESSING_EC_CODE = 303;
      OPEN_MENU_SCREEN_EC_CODE = 351;
      OPEN_SAVE_SCREEN_EC_CODE = 352;
      GAME_OVER_EC_CODE = 353;
      RETURN_TO_TITLE_SCREEN_EC_CODE = 354;

      CHANGE_BATTLE_BGM_EC_CODE = 132;
      CHANGE_VICTORY_ME_EC_CODE = 133;
      CHANGE_DEFEAT_ME_EC_CODE = 139;
      CHANGE_VEHICLE_BGM_EC_CODE = 140;
      CHANGE_SAVE_ACCESS_EC_CODE = 134;
      CHANGE_MENU_ACCESS_EC_CODE = 135;
      CHANGE_ENCOUNTER_EC_CODE = 136;
      CHANGE_FORMATION_ACCESS_EC_CODE = 137;
      CHANGE_WINDOW_COLOUR_EC_CODE = 138;
      CHANGE_ACTOR_IMAGES_EC_CODE = 322;
      CHANGE_VEHICLE_IMAGE_EC_CODE = 323;

      CHANGE_MAP_NAME_DISPLAY_EC_CODE = 281;
      CHANGE_TILESET_EC_CODE = 282;
      CHANGE_BATTLEBACK_EC_CODE = 283;
      CHANGE_PARALLAX_EC_CODE = 284;
      GET_LOCATION_INFO_EC_CODE = 285;

      CHANGE_ENEMY_HP_EC_CODE = 331;
      CHANGE_ENEMY_MP_EC_CODE = 332;
      CHANGE_ENEMY_TP_EC_CODE = 342;
      CHANGE_ENEMY_STATE_EC_CODE = 333;
      COMPLETE_ENEMY_RECOVERY_EC_CODE = 334;
      ENEMY_APPEAR_EC_CODE = 335;
      ENEMY_TRANSFORM_EC_CODE = 336;
      SHOW_BATTLE_ANIMATION_EC_CODE = 337;
      FORCE_ACTION_EC_CODE = 339;
      ABORT_BATTLE_EC_CODE = 340;

      SCRIPT_EC_CODE = 355;
      SCRIPT_LINE_EC_CODE = 655;
      PLUGIN_COMMAND_EC_CODE = 356;
    }
    PLUGIN_COMMAND_EC_CODE: Result := TECPluginCommandFrame;
  else
    Result := nil;
  end;
end;

function GetEditorForEc(Ec: TJSONObject): TEcFrameClass;
var
  CodeJson: TJSONNumber;
begin
  if (Ec <> nil) and Ec.Find('code', CodeJson) then
    Result := GetEditorForEc(CodeJson.AsInteger)
  else
    Result := nil;
end;

function IsCommandWithoutParameters(Code: Integer): Boolean;
begin
  Result := (Code = 0)
    or (Code = LOOP_EC_CODE)
    or (Code = BREAK_LOOP_EC_CODE)
    or (Code = EXIT_EVENT_PROCESSING_EC_CODE)
    or (Code = GET_ON_OFF_VEHICLE_EC_CODE)
    or (Code = GATHER_FOLLOWERS_EC_CODE)
    or (Code = ERASE_EVENT_EC_CODE)
    or (Code = FADEOUT_SCREEN_EC_CODE)
    or (Code = FADEIN_SCREEN_EC_CODE)
    or (Code = SAVE_BGM_EC_CODE)
    or (Code = REPLAY_BGM_EC_CODE)
    or (Code = STOP_SE_EC_CODE)
    or (Code = OPEN_MENU_SCREEN_EC_CODE)
    or (Code = OPEN_SAVE_SCREEN_EC_CODE)
    or (Code = GAME_OVER_EC_CODE)
    or (Code = RETURN_TO_TITLE_SCREEN_EC_CODE)
end;

end.

